package main.java;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

/**
 * @Author: GJH
 */
public class NGuessNumber {

    public static int[] playerInput = new int[4];

    /**
     * 随机生成4个0-9之间的不重复的整型数作为答案并返回的功能
     *
     * @author GJH
     */
    public static String generateAnswer() {
        Random rand = new Random();
        int i1 = rand.nextInt(10);
        int i2 = rand.nextInt(10);
        while (i2 == i1) {
            i2 = rand.nextInt(10);
        }
        int i3 = rand.nextInt(10);
        while (i3 == i1 || i3 == i2) {
            i3 = rand.nextInt(10);
        }
        int i4 = rand.nextInt(10);
        while (i4 == i1 || i4 == i2 || i4 == i3) {
            i4 = rand.nextInt(10);
        }
        String s1 = String.valueOf(i1);
        String s2 = String.valueOf(i2);
        String s3 = String.valueOf(i3);
        String s4 = String.valueOf(i4);

        String s = s1 + s2 + s3 + s4;

        //System.out.println("答案：" + s);
        return s;
    }

    /**
     * 接收玩家输入的4个数字并返回
     *
     * @author GJH
     */
    public static String getPlayerInput() {
        Scanner scanner = new Scanner(System.in);
        return scanner.nextLine();
    }

    /**
     * 比较玩家猜测和答案后返回提示信息
     *
     * @author GJH
     */
    public static String compareGuessAnswer(String playerInput, String answer) {
        String s = "参数有误，请检测";
        if (playerInput.length() == 4 && answer.length() == 4) {
            List<String> p = new ArrayList<>();

            String p1 = playerInput.substring(0, 1);
            String p2 = playerInput.substring(1, 2);
            String p3 = playerInput.substring(2, 3);
            String p4 = playerInput.substring(3, 4);

            p.add(p1);
            p.add(p2);
            p.add(p3);
            p.add(p4);

            String a1 = answer.substring(0, 1);
            String a2 = answer.substring(1, 2);
            String a3 = answer.substring(2, 3);
            String a4 = answer.substring(3, 4);

            List<String> a = new ArrayList<>();

            a.add(a1);
            a.add(a2);
            a.add(a3);
            a.add(a4);

            int countP = 0;
            int countB = 0;
            for (int i = 0; i < 4; i++) {
                if (p.get(i).equals(a.get(i))) {
                    countP++;
                } else if (a.indexOf(p.get(i)) > -1) {
                    countB++;
                }
            }

            s = countP + "A" + countB + "B";
        }
        return s;
    }

    /**
     * 根据提示信息中A的数量判断玩家是否获胜，如果获胜返回true，否则返回false
     *
     * @author GJH
     */
    public static boolean isWin(String message) {
        boolean flag = false;
        if (message.indexOf("4A") > -1) {
            flag = true;
        }
        return flag;
    }

    public static void main(String[] args) {
        int maxCount = 20;
        int curCount = maxCount;
        System.out.println("===========欢迎来到新猜数字小游戏=========");
        System.out.println("游戏玩法：根据提示，猜对4个数字和他们的顺序即可获胜！" + curCount + "次猜测机会");
        System.out.println("请输入4个0-9之间的数字：");

        String answer = generateAnswer();

        boolean gameFlag = true;

        boolean continueFlag = true;
        do {
            String playerInput = getPlayerInput();
            if (gameFlag) {
                if (curCount > 0) {
                    int length = playerInput.length();
                    int count = 4;
                    if (length == count) {
                        String result = compareGuessAnswer(playerInput, answer);
                        if (isWin(result)) {
                            gameFlag = false;
                            System.out.println("恭喜你获胜了");
                            System.out.println("是否再来一局？y/n?");
                        } else {
                            curCount--;
                            System.out.println("提示信息：" + result + "，还有" + curCount + "次猜测机会");
                            System.out.println("A表示存在且位置正确的数字个数；B表示存在且位置不正确的数字个数！");
                            System.out.println("如：1A2B，标识猜测的4个数字中有3个是对的，有1个位置对，有2个位置不对。\r\n");

                            if (curCount <= 0) {
                                gameFlag = false;
                                System.out.println("很遗憾失败了，正确答案：" + answer);
                                System.out.println("是否再来一局？y/n?");
                            } else {
                                System.out.println("请输入4个0-9之间的数字：");
                            }
                        }
                    } else {
                        System.out.println("请输入4个0-9之间的数字：");
                    }
                }
            } else {
                if ("y".equals(playerInput) || "Y".equals(playerInput)) {
                    System.out.println("游戏重新开始");
                    System.out.println("请输入4个0-9之间的数字：");
                    gameFlag = true;
                    curCount = maxCount;
                    answer = generateAnswer();
                } else if ("n".equals(playerInput) || "N".equals(playerInput)) {
                    System.out.println("游戏结束");
                    continueFlag = false;
                } else {
                    System.out.println("是否再来一局？y/n?");
                }
            }


        }
        while (continueFlag);

    }
}
